package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    //批量导入预约
    @Override
    public void add(List<OrderSetting> list) {

        if (list != null && list.size() > 0) {
            //遍历每一天的数据
            for (OrderSetting orderSetting : list) {
                //判断是否今天之前的数据
                if (orderSetting.getOrderDate().compareTo(new Date()) == -1) {
                    //是，跳过这次循环
                    continue;
                }

                //查询数据这一天的数据是否存在
                Long count=orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    //表示数据存在，执行修改操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //表达数据不存在，执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }

        }

    }

    //根据月份查询该月的数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {

        String dateBegin =date+"-1";//2019-05-1
        String dateEnd =date+"-31";//2019-05-31
        HashMap map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        //查询数据亏获取返回的数据
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        ArrayList<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            HashMap hashMap = new HashMap();
            hashMap.put("date",orderSetting.getOrderDate().getDate());//获取几号
            hashMap.put("number",orderSetting.getNumber());//获取可以约多少人
            hashMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(hashMap);
        }

        return data;
    }

    //根据指定日期修改可预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        Long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            //当前日期已经了预约设置，需要进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }

    }


}
